/*
 * Created on 23.07.2008
 *
 */
package com.appspot.yttv.client;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.IncrementalCommand;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ChangeListenerCollection;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SourcesChangeEvents;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 *  PlaylistWidget shows a playlist.
 *  
 * @author Uwe Maurer
 *
 */
public class PlaylistWidget extends Composite implements SourcesChangeEvents  {
	private Panel panel = new VerticalPanel();	
	private Playlist playlist;
	private final ChangeListenerCollection changeListeners = new ChangeListenerCollection();
	private Map<Video, Panel> rows = new HashMap<Video, Panel>();
	private Video highlightVideo;
	private final ClickListener listener;
	private Video clickVideo;
	
	public PlaylistWidget(ClickListener listener) {
		initWidget(panel);
		this.listener = listener;
	}

	public void setPlaylist(Playlist playlist) {
		this.playlist = playlist;
		update();
	}	
	
	public Playlist getPlaylist() {
		return playlist;
	}
	
	public void update() {
		panel.clear();
		rows.clear();
		final Playlist playlist = this.playlist;
		
		DeferredCommand.addCommand(new IncrementalCommand() {
			private int pos = 0;
			public boolean execute() {
				Video video = playlist.getVideos().get(pos);
				addVideoWidget(video);
				highlightVideo(highlightVideo);
				return ++pos < playlist.getVideos().size();
			}
		});
	}
	
	public void addChangeListener(ChangeListener listener) {
		changeListeners.add(listener);
	}

	public void removeChangeListener(ChangeListener listener) {
		changeListeners.remove(changeListeners);
	}
	
	public void highlightVideo(Video video)  {
		Panel panel = rows.get(highlightVideo);
		if (panel != null) {
			panel.setStyleName("row");	
		}
		highlightVideo = video;
		panel = rows.get(highlightVideo);
		if (panel != null) {
			panel.setStyleName("active-row");	
		}
	}
	
	public Video getClickedVideo() {
		return clickVideo;
	}

	public void addVideo(Video video) {
		playlist.addVideo(video);
		addVideoWidget(video);
	}
	
	private  void addVideoWidget(final Video video) {
		Panel row = new HorizontalPanel();
		Image img = new Image(video.getThumbnail());
		img.setStyleName("thumbnail");
		row.add(img);
		HTML label = new HTML(video.getTitle() + " &nbsp;<i>(" + video.getChannel()+")</i>");
		label.setStyleName("preview");
		row.add(label);
		panel.add(row);
		rows.put(video, row);
		panel.setStyleName("row");
		if (listener != null) {
			img.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					clickVideo = video;
					listener.onClick(PlaylistWidget.this);
				}
				
			});
		}
	}
	
}
